package coupon

import (
	"github.com/gin-gonic/gin"
	"shop/formdata"
	"shop/internal/controller/base"
	coupon1 "shop/internal/models/coupon"
)

// 下单时优惠卷展示
func GoodsOrderCouponList(c *gin.Context) {
	var coupon formdata.GoodsOrderCouponList
	if err := c.ShouldBindJSON(&coupon); err != nil {
		base.RespData(c, nil, err)
		return
	}

	c2 := new(coupon1.Coupon)
	listShop, err := c2.CouponListShop(int(coupon.UserId), coupon.Judge)
	if err != nil {
		return
	}

	base.RespData(c, listShop, nil)
}

// 用户中心优惠卷展示
func UserCenterCouponList(c *gin.Context) {
	var coupon formdata.UserCenterCouponList
	if err := c.ShouldBindJSON(&coupon); err != nil {
		base.RespData(c, nil, err)
		return
	}

	c2 := new(coupon1.Coupon)
	listShop, err := c2.CouponListUser(int(coupon.UserId), int(coupon.CouponStatus))
	if err != nil {
		return
	}

	base.RespData(c, listShop, nil)
}

// 优惠卷单条新增
func CouponPointsAdd(c *gin.Context) {
	var coupon formdata.CouponPoints
	if err := c.ShouldBindJSON(&coupon); err != nil {
		base.RespData(c, nil, err)
		return
	}

	c2 := new(coupon1.Coupon)
	listShop, err := c2.CouponAdd(coupon.UserId, coupon.PointsId)
	if err != nil {
		return
	}

	base.RespData(c, listShop, nil)
}

// 优惠卷详情
func CouponInfoByID(c *gin.Context) {
	var coupon formdata.CouponInfo
	if err := c.ShouldBindJSON(&coupon); err != nil {
		base.RespData(c, nil, err)
		return
	}

	c2 := new(coupon1.Coupon)
	listShop, err := c2.CouponInfoByCouponID(coupon.CouponId)
	if err != nil {
		return
	}

	base.RespData(c, listShop, nil)
}

// 优惠卷多条新增
func CouponVipMultipleAdd(c *gin.Context) {
	var rule []coupon1.Rule
	var coupon formdata.CouponVip
	if err := c.ShouldBindJSON(&coupon); err != nil {
		base.RespData(c, nil, err)
		return
	}

	if err := c.ShouldBindJSON(&rule); err != nil {
		base.RespData(c, nil, err)
		return
	}

	c2 := new(coupon1.Coupon)
	listShop, err := c2.CouponCreate(coupon.UserId, rule)
	if err != nil {
		return
	}

	base.RespData(c, listShop, nil)
}

// 规则表列表
func MemberList(c *gin.Context) {
	c2 := new(coupon1.Rule)
	listShop, err := c2.RuleList()
	if err != nil {
		return
	}

	base.RespData(c, listShop, nil)
}

// 兑换优惠卷列表详情
func PointsList(c *gin.Context) {
	c2 := new(coupon1.PointsCoupon)
	listShop, err := c2.PointsList()
	if err != nil {
		return
	}

	base.RespData(c, listShop, nil)
}

// 兑换优惠卷详情
func PointsInfo(c *gin.Context) {
	var coupon formdata.PointsInfo
	if err := c.ShouldBindJSON(&coupon); err != nil {
		base.RespData(c, nil, err)
		return
	}

	c2 := new(coupon1.PointsCoupon)
	listShop, err := c2.PointsInfoByID(coupon.PointsID)
	if err != nil {
		return
	}

	base.RespData(c, listShop, nil)
}
